package binaryTree;

import java.util.ArrayList;
import java.util.List;

/*
 *@description 二叉搜索树的最小绝对差
 *@author hkfan
 *@version 1.0
 *@create 2025/6/12 09:25
 */
public class LeetCode_530 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public int getMinimumDifference(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        solution(root,list);
        int res = Integer.MAX_VALUE,cur=list.get(0);
        for (int i = 1; i < list.size(); i++) {
            res = Math.min(res,Math.abs(cur-list.get(i)));
            cur= list.get(i);
        }
        return res;
    }

    private void solution(TreeNode root, List<Integer> list) {
        if (root==null){
            return;
        }
        if (root.left!=null){
            solution(root.left,list);
        }
        list.add(root.val);
        if (root.right!=null){
            solution(root.right,list);
        }
    }

}
